import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

class MultiLineInputWidget extends StatefulWidget {
  const MultiLineInputWidget({
    super.key,
    this.margin,
    this.padding,
    this.radius,
    this.bgColor,
    this.placeholder,
    this.placeholderStyle,
    this.style,
    this.controller,
    this.width,
    this.height,
    this.maxLength,
    this.maxLines,
    this.focusNode,
    this.showCursor = false,
    this.defaultText,
  });

  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final double? width;
  final double? height;
  final double? radius;
  final Color? bgColor;
  final String? placeholder;
  final TextStyle? placeholderStyle;
  final TextStyle? style;
  final TextEditingController? controller;
  final int? maxLength;
  final int? maxLines;
  final FocusNode? focusNode;
  final bool showCursor;
  final String? defaultText;

  @override
  State<MultiLineInputWidget> createState() => _MultiLineInputWidgetState();
}

class _MultiLineInputWidgetState extends State<MultiLineInputWidget> {
  late FocusNode actualFocusNode;
  late TextEditingController actualController;

  @override
  void initState() {
    super.initState();

    actualFocusNode = widget.focusNode ?? FocusNode();
    actualController = widget.controller ?? TextEditingController();

    if (widget.showCursor) {
      Future.delayed(Duration.zero, () {
        FocusScope.of(Get.context!).requestFocus(actualFocusNode);
      });
    }
    if ((widget.defaultText != null) && (widget.defaultText!.isNotEmpty)) {
      actualController.text = widget.defaultText!;
    }
  }

  @override
  void dispose() {
    if (widget.controller == null) {
      actualController.dispose();
    }
    if (widget.focusNode == null) {
      actualFocusNode.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      margin: widget.margin,
      padding: widget.padding ?? EdgeInsets.symmetric(horizontal: 12.r, vertical: 8.r),
      decoration: BoxDecoration(
        color: widget.bgColor ?? ColorUtils.c_f8f8f8,
        borderRadius: BorderRadius.circular(widget.radius ?? 4.r),
      ),
      child: TextField(
        controller: actualController,
        style: widget.style ?? StyleUtils.ts_33_13_500,
        decoration: InputDecoration(
          hintText: widget.placeholder ?? "please_input".tr,
          hintStyle: widget.placeholderStyle ?? StyleUtils.ts_99_13_400,
          border: InputBorder.none,
          focusedBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          errorBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
        ),
        maxLines: widget.maxLines ?? 5,
        maxLength: widget.maxLength ?? 200,
        focusNode: actualFocusNode,
      ),
    );
  }
}
